﻿using Aspose.Cells;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace Excel合并程序
{
    class Program
    {
        //固定文件名
        static string name = $"{DateTime.Now.ToString("D")}.xlsx";
        static int countNum = 0;
        static string[] paths = null;
        static int NumSave = 0;
        //static string[] paths = { @"E:\程序文件夹\Excel合并程序\Excel合并程序\bin\Debug\新建文件夹\阳光厚度40%SOC.xlsx" };
        static DataIn dataList = null;
        static void Main(string[] args)
        {
            try
            {
                paths = Directory.GetFiles(args[0], "*.xlsx");
            }
            catch (Exception)
            {
                paths = args;
                paths = paths.ToList().Where(x => x.EndsWith(".xlsx")).ToArray();
            }
            if (paths.Length==0)
            {
                Console.WriteLine($"【{DateTime.Now.ToString("T")}】错误 -> 请检查文件类型！");
                Console.ReadKey();
                return;
            }
            Console.WriteLine($"【{DateTime.Now.ToString("T")}】读取 -> {paths.Length}个文件");
            //文件路径
            foreach (var path in paths)
            {
                Console.WriteLine($"【{DateTime.Now.ToString("T")}】开始读取 -> {Path.GetFileName(path)}");
                Workbook work = new Workbook(path);
                //表的数量，读取所有表，进行合并写入
                for (int i = 0; i < work.Worksheets.Count; i++)
                {
                    Console.WriteLine($"【{DateTime.Now.ToString("T")}】表数 -> {work.Worksheets.Count}张");
                    List<DataIn> datas = new List<DataIn>();
                    Worksheet sheet = work.Worksheets[i];
                    Console.WriteLine($"【{DateTime.Now.ToString("T")}】行数 -> {sheet.Cells.MaxDataRow + 1}行");
                    Console.WriteLine($"【{DateTime.Now.ToString("T")}】列数 -> {sheet.Cells.MaxDataColumn + 1}列");

                    //读取行和列
                    int row = sheet.Cells.MaxDataRow + 1;
                    int column = sheet.Cells.MaxDataColumn + 1;
                    //行 非要加1，不是很懂，不加1会出问题
                    for (int j = 0; j < row; j++)
                    {
                        dataList = new DataIn();
                        //列 非要加1，不是很懂，不加1会出问题
                        for (int k = 0; k < column; k++)
                        {
                            dataList.Data.Add(sheet.Cells[j, k].StringValue);
                        }
                        datas.Add(dataList);
                        if (j%10000==0&&j!=0)
                        {
                            if (countNum == 0)
                            {
                                Console.WriteLine($"【{DateTime.Now.ToString("T")}】检测超过限制 -> 逐步写入");
                                //第一次写入
                                AllToOneTime(datas);
                                countNum++;
                                Console.WriteLine($"【{DateTime.Now.ToString("T")}】开始读取 -> {Path.GetFileName(path)} -> 写入成功！");
                                datas = new List<DataIn>();
                            }
                            else
                            {
                                Console.WriteLine($"【{DateTime.Now.ToString("T")}】检测超过限制 -> 逐步写入");
                                AllToOne(datas);
                                Console.WriteLine($"【{DateTime.Now.ToString("T")}】开始读取 -> {Path.GetFileName(path)} -> 写入成功！");
                                datas = new List<DataIn>();
                            }
                        }
                    }
                    if (countNum == 0)
                    {
                        //第一次写入
                        AllToOneTime(datas);
                        countNum++;
                        Console.WriteLine($"【{DateTime.Now.ToString("T")}】开始读取 -> {Path.GetFileName(path)} -> 写入成功！");
                    }
                    else
                    {
                        AllToOne(datas);
                        Console.WriteLine($"【{DateTime.Now.ToString("T")}】开始读取 -> {Path.GetFileName(path)} -> 写入成功！");
                    }
                }
            }
            Console.WriteLine($"【{DateTime.Now.ToString("T")}】 全部合并成功！");
            Console.ReadKey();
        }

     
        static void AllToOneTime(List<DataIn> dataList)
        {
            Workbook book = new Workbook();
            Worksheet sheet = book.Worksheets[0];

            //集合数量
            int dataListCount = dataList.Count;
            for (int i = 0; i < dataListCount; i++)
            {
                //第一行列的长度，进行循环
                for (int j = 0; j < dataList[0].Data.Count; j++)
                {
                    sheet.Cells[i, j].PutValue(dataList[i].Data[j]);
                }
                NumSave++;
            }
            book.Save(name);
        }

        static void AllToOne(List<DataIn> dataList)
        {
            Workbook book = new Workbook(name);
            Worksheet sheet = book.Worksheets[0];

            //集合数量
            int dataListCount = dataList.Count;
            for (int i = 0; i < dataListCount; i++)
            {
                //第一行列的长度，进行循环
                for (int j = 0; j < dataList[0].Data.Count; j++)
                {
                    sheet.Cells[NumSave, j].PutValue(dataList[i].Data[j]);
                }
                NumSave++;
            }
            book.Save(name);
        }
    }
}
